package com.winning.cqs.pregnantbabycare_cqs_v1.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

public class ObservableHorizontalScrollView extends HorizontalScrollView {
	private onMyScrollListner onScrollListner;
	private Runnable scrollerTask;
	private int intitPosition;
	private int newCheck = 100;

	public ObservableHorizontalScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		scrollerTask = new Runnable() {
			@Override
			public void run() {
				int newPosition = getScrollX();
				if (intitPosition - newPosition == 0) {
					if (onScrollListner == null) {
						return;
					}
					onScrollListner.onScrollChange(getScrollX());
				} else {
					intitPosition = getScrollX();
					postDelayed(scrollerTask, newCheck);
				}
			}
		};
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub
		super.onScrollChanged(l, t, oldl, oldt);
		onScrollListner.onScrollChange(l);
	}

	public ObservableHorizontalScrollView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	public ObservableHorizontalScrollView(Context context) {
		super(context);

	}

	public interface onMyScrollListner {
		void onScrollChange(int index);
	}

	public void setOnScrollListner(onMyScrollListner MyScrollListner) {
		onScrollListner = MyScrollListner;
	}

	public void startScrollerTask() {
		intitPosition = getScrollX();
		postDelayed(scrollerTask, newCheck);
	}

}
